package com.huatai.saas.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.huatai.common.api.R;
import com.huatai.saas.dto.*;
import com.huatai.saas.entity.SceneFileEntity;
import com.huatai.saas.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import com.huatai.saas.service.SceneFileService;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * @BelongsProject: nanming-saas
 * @BelongsPackage: com.huatai.saas.controller
 * @Author: hecaiy
 * @CreateTime: 2024-04-28  16:48
 * @Description: TODO
 * @Version: 1.0
 */
@RestController
@Api(value = "场景文件管理", tags = "场景文件管理")
@RequestMapping("/scene/manage")
public class SceneFileManageController {
	@Autowired
	private SceneFileService sceneFileService;

//	@ApiOperation(value = "保存或更新场景文件弃用")
//	@PostMapping("/saveOrUpdateSceneFile")
//	public R saveOrUpdateSceneFile(@RequestBody SceneFileEntity sceneFileEntity) {
//		sceneFileService.saveOrUpdateSceneFile(sceneFileEntity);
//		return R.success("操作成功");
//	}

	@ApiOperation(value = "运营端保存场景文件")
	@PostMapping("/saveSceneFile")
	public R<Long> saveSceneFile(@Valid @RequestBody SceneFileDto sceneFileEntity) {
		return R.data(sceneFileService.saveSceneFile(sceneFileEntity));
	}

	@ApiOperation(value = "运营端更新场景文件路径")
	@PostMapping("/updateSceneFilePath")
	public R<Boolean> updateSceneFilePath(@RequestBody SceneFileEntity sceneFileEntity) {
		return R.data(sceneFileService.updateSceneFilePath(sceneFileEntity));
	}

	@ApiOperation(value = "运营端更新场景基本信息")
	@PostMapping("/updateSceneFile")
	public R<Boolean> updateSceneFile(@Valid @RequestBody SceneFileDto sceneFileEntity) {
		return R.data(sceneFileService.updateSceneFile(sceneFileEntity));
	}

	@ApiOperation(value = "运营端删除场景文件")
	@PostMapping("/delFileListByIds")
	public R<String> delFileListByIds(@RequestBody Long[] ids) {
		sceneFileService.delFileListByIds(Arrays.asList(ids));
		return R.success("操作成功");
	}

	@ApiOperation(value = "运营端分页获取场景列表数据")
	@PostMapping("/getSceneFileList")
	public R<IPage<SceneFileEntity>> getSceneFileList(@RequestBody BackFileListQueryDto dto) {
		return R.data(sceneFileService.getFilePageList(dto));
	}

	@ApiOperation(value = "运营端授权用户场景")
	@PostMapping("/authSceneUsers")
	public R<Boolean> authSceneUsers(@Valid @RequestBody AuthSceneUserDto dto) {
		return R.data(sceneFileService.authSceneUsers(dto));
	}

	@ApiOperation(value = "运营端取消用户授权场景")
	@PostMapping("/disableSceneUser")
	public R<Boolean> disableSceneUser(@RequestBody CanalSceneDto dto) {
		return R.data(sceneFileService.disableSceneUser(dto.getScId(), dto.getUserId()));
	}

	@ApiOperation(value = "用户端获取场景历史版本")
	@PostMapping("/getSceneFileHistoryList")
	public R<List<SceneFileHistoryInfoVo>> getSceneFileHistoryList(@RequestBody DisableSceneDto dto) {
		return R.data(sceneFileService.getSceneFileHistoryList(dto.getId()));
	}

	@ApiOperation(value = "客户端获取场景历史版本")
	@PostMapping("/getClientSceneFileHistoryList")
	public R<List<SceneFileHistoryInfoVo>> getClientSceneFileHistoryList(@RequestBody DisableSceneDto dto) {
		return R.data(sceneFileService.getSceneFileHistoryList(dto.getId()));
	}

	@ApiOperation(value = "客户端用户场景列表")
	@PostMapping("/getClientSceneListByUserId")
	public R<IPage<SceneFileAuthUserVo>> getClientSceneListByUserId(@RequestBody  BackFileListQueryDto dto) {
		return R.data(sceneFileService.getPageSceneListByUserId(dto));
	}

	@ApiOperation(value = "客户端获取签名,将签名以文件留的形式保存到相关场景目录")
	@PostMapping("/getClientSignature")
	public R<String> getClientSignature(@RequestBody  DisableSceneDto dto) {
		return R.data(sceneFileService.getSignature(dto.getId()));
	}

	@ApiOperation(value = "用户端获取用户被授权的场景列表")
	@PostMapping("/getSceneListByUserId")
	public R<IPage<SceneFileAuthUserVo>> getSceneListByUserId(@RequestBody  BackFileListQueryDto dto) {
		return R.data(sceneFileService.getPageSceneListByUserId(dto));
	}

	@ApiOperation(value = "用户端收藏场景文件")
	@PostMapping("/collectSceneFile")
	public R<Boolean> collectSceneFile(@RequestBody DisableSceneDto dto) {
		return R.data(sceneFileService.collectSceneFile(dto.getId()));
	}

	@ApiOperation(value = "用户端取消收藏场景文件")
	@PostMapping("/cancelCollectSceneFile")
	public R<Boolean> cancelCollectSceneFile(@RequestBody DisableSceneDto dto) {
		return R.data(sceneFileService.cancelCollectSceneFile(dto.getId()));
	}

	@ApiOperation(value = "用户端获取用户收藏的场景列表")
	@PostMapping("/getSceneCollectListByUserId")
	public R<IPage<SceneFileCollectUserVo>> getSceneCollectListByUserId(@RequestBody  BackFileListQueryDto dto) {
		return R.data(sceneFileService.getPageSceneCollectListByUserId(dto));
	}

	@ApiOperation(value = "运营端根据用户id  获取场景列表，并判断场景是否被授权")
	@PostMapping("/getIsAuthPageData")
	public R<IPage<SceneFileAuthInfoVo>> getIsAuthPageData(@RequestBody  AuthSceneListQueryDto dto) {
		return R.data(sceneFileService.getIsAuthPageData(dto));
	}

	@ApiOperation(value = "运营端根据用户id获取用户已授权列表")
	@PostMapping("/getByUserIdPageData")
	public R<IPage<SceneFileAuthInfoVo>> getByUserIdPageData(@RequestBody  AuthSceneListQueryDto dto) {
		return R.data(sceneFileService.getByUserIdPageData(dto));
	}

	@ApiOperation(value = "用户端获取云场景列表")
	@PostMapping("/getCollectPageData")
	public R<IPage<SceneFileAuthInfoVo>> getCollectPageData(@RequestBody  AuthSceneListQueryDto dto) {
		return R.data(sceneFileService.getCollectPageData(dto));
	}

	@ApiOperation(value = "UE客户端调用场景授权信息")
	@PostMapping("/getAuthorizationInfo")
	public R<AuthorizationVO> getAuthorizationInfo(@RequestBody AuthorizationDto dto) {
		return R.data(sceneFileService.getSceneFileAuthInfo(dto.getSignature(),dto.getOpenId()));
	}

	@ApiOperation(value = "客户端下载场景文件")
	@GetMapping("/downloadScene")
	//public R downloadScene(@RequestBody DowmloadParamDto dto) {
	public R downloadScene(@ApiParam(name = "usaId",value = "用户场景授权id",required = true) @RequestParam(name = "usaId") Long usaId) {
		//sceneFileService.download(dto.getUsaId());
		sceneFileService.download(usaId);
		return R.success("操作成功");
	}

	@ApiOperation(value = "运营端下载场景文件")
	@GetMapping("/downloadMessanger")
	public R downloadMessanger(@ApiParam(name = "id",value = "场景id",required = true) @RequestParam(name = "id") Long id) {
		sceneFileService.downloadMessanger(id);
		return R.success("操作成功");
	}

	@ApiOperation(value = "客户端下载场景历史版本")
	@GetMapping("/downloadHistoryScene")
	//public R<String> downloadHistoryScene(@RequestBody DisableSceneDto dto) {
	public R<String> downloadHistoryScene(@ApiParam(name = "id",value = "场景id",required = true) @RequestParam(name = "id") Long id,
										  @ApiParam(name = "version",value = "场景版本号",required = true) @RequestParam(name = "version") String version) {
		//sceneFileService.downloadHistoryScene(dto.getId());
		sceneFileService.downloadHistoryScene(id,version);
		return R.success("操作成功");
	}

	@ApiOperation(value = "客户端获取历史场景分片数量")
	@GetMapping("/getFilePartHistoryNums")
	public R<Integer> getFilePartHistoryNums(@ApiParam(name = "id",value = "场景id",required = true) @RequestParam(name = "id") Long id,
											 @ApiParam(name = "version",value = "场景版本号",required = true) @RequestParam(name = "version") String version) {
		return R.data(sceneFileService.getFilePartHistoryNums(id,version));
	}

	@ApiOperation(value = "客户端分片下载历史场景文件")
	@GetMapping("/partDowmloadHistoryScene")
	public R<String> partDowmloadHistoryScene(@ApiParam(name = "id",value = "场景id",required = true) @RequestParam(name = "id") Long id,
											  @ApiParam(name = "version",value = "场景版本号",required = true) @RequestParam(name = "version") String version,
											  @ApiParam(name = "partIndex",value = "分片数",required = true) @RequestParam(name = "partIndex") Integer partIndex) {
		sceneFileService.partDowmloadHistoryScene(id,version,partIndex);
		return R.success("操作成功");
	}

	@ApiOperation(value = "客户端分片下载场景文件")
	@GetMapping("/partDowmloadScene")
	//public R<String> partDowmloadScene(@RequestBody DowmloadPartDto dto) {
	public R<String> partDowmloadScene(@ApiParam(name = "id",value = "场景id",required = true) @RequestParam(name = "id") Long id,
									   @ApiParam(name = "partIndex",value = "分片数",required = true) @RequestParam(name = "partIndex") Integer partIndex) {
		//sceneFileService.partDowmloadScene(dto.getId(),dto.getPartIndex());
		sceneFileService.partDowmloadScene(id,partIndex);
		return R.success("操作成功");
	}

	@ApiOperation(value = "客户端获取场景文件分片数量")
	@PostMapping("/getFilePartNums")
	public R<Integer> getFilePartNums(@RequestBody DisableSceneDto dto) {
		return R.data(sceneFileService.getFilePartNums(dto.getId()));
	}

	@ApiOperation(value = "运营端、用户端获取场景文件信息")
	@PostMapping("/getSceneFileInfo")
	public R<SceneFileEntityVo> getSceneFileInfo(@RequestBody DisableSceneDto dto) throws Exception {
		return R.data(sceneFileService.getSceneFileInfo(dto.getId()));
	}

	@ApiOperation(value = "客户端获取场景文件信息")
	@PostMapping("/getClintSceneFileInfo")
	public R<SceneFileEntityVo> getClintSceneFileInfo(@RequestBody ClientSceneDto dto) {
		return R.data(sceneFileService.getClintSceneFileInfo(dto.getUsaId()));
	}
}
